// pages/joinGroup/joinGroup.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{

    },
    joinGroupInfo:{

    },
    name:null,
    phone:null,
    quantity: 1,
    totalPrice: 0
  },
  // 查询加入拼团信息
  handleFindById(id){
    wx.request({
      url: `http://localhost:9301/group/findJoinGroupInfo?id=${id}`,
      method: 'get',
      success: (res)=>{
        this.setData({
          joinGroupInfo : res.data.data,
          totalPrice: res.data.data.group.groupPrice
        })
        console.log("赋值后:",this.data.joinGroupInfo);
      }
    })
  },
  // 加入拼团页面跳转
  handleJoinGroup(){
    if(this.data.phone == null || this.data.name == null){
      wx.showToast({
        icon: 'error',
        title: '信息不能为空',
        duration: 1500
      })
      return null;
    }
    const regex = /^1[3-9]\d{9}$/;
    if (!regex.test(this.data.phone)) {
      wx.showToast({
        icon:"error",
        title: '手机号有误',
      })
      this.setData({
        phone:""
      })
    }else{
      const data = {
        groupId: this.data.joinGroupInfo.group.id,
        storeId: this.data.joinGroupInfo.store.id,
        leaderId: this.data.joinGroupInfo.leader.id,
        userId: this.data.userInfo.id,
        name: this.data.name,
        phone: this.data.phone,
        quantity: this.data.quantity
      };
      console.log(data);
      wx.request({
        url:`http://localhost:9301/group/joinGroup`,
        method:"post",
        data:{
          groupId: this.data.joinGroupInfo.group.id,
          storeId: this.data.joinGroupInfo.store.id,
          leaderId: this.data.joinGroupInfo.leader.id,
          userId: this.data.userInfo.id,
          name: this.data.name,
          phone: this.data.phone,
          quantity: this.data.quantity
        },
        success: (res)=>{
          if(!res.data.flag){
            wx.showModal({
              title: '提示',
              content: res.data.message,
              complete: (res) => {
                if (res.cancel) {
                  
                }
                if (res.confirm) {
                  
                }
              }
            })
            return null;
          }else{
            wx.navigateTo({
              url: `/pages/joinGroupSuccess/joinGroupSuccess?orderId=${res.data.data}`,
            })
          }
          console.log("确认拼团后的结果:",res);
        }
      })
    }
  },
  // 输入的预留电话
  handleInputPhone(e){
    const phone = e.detail.value
    this.setData({
      phone: phone
    })
  
  },
  // 输入的预留名称
  handleInputName(e){
    const name = e.detail.value
    this.setData({
      name: name
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options.id
    this.handleFindById(id);
    wx.getStorage({
      key: "userInfo",
      success: function (res) {
        this.setData({
          userInfo: res.data
        });
      }.bind(this) // 使用 bind 方法绑定 this
    });
    // 初始化总价
    this.calculateTotal();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 减少数量
  handleReduce() {
    if (this.data.quantity <= 1) return;
    this.setData({
      quantity: this.data.quantity - 1
    }, () => {
      this.calculateTotal();
    });
  },

  // 增加数量
  handleAdd() {
    if(this.data.quantity < this.data.joinGroupInfo.group.limitBuy){
      this.setData({
        quantity: this.data.quantity + 1
      }, () => {
        this.calculateTotal();
      });
    }else{
      wx.showModal({
        title: '提示',
        content: "每人限购"+this.data.joinGroupInfo.group.limitBuy+"份",
        complete: (res) => {
          if (res.cancel) {
            
          }
          if (res.confirm) {
            
          }
        }
      })
    }
  },

  // 计算总价
  calculateTotal() {
    const total = (this.data.joinGroupInfo.group.groupPrice * this.data.quantity).toFixed(2);
    this.setData({
      totalPrice: total
    });
  }
})